How does one Blockchain?
“A blockchain is essentially a distributed database of records or public ledger of all transactions or digital events that have been executed and shared among participating parties. Each transaction in the public ledger is verified by consensus of a majority of the participants in the system. And, once entered, information can never be erased. The blockchain contains a certain and verifiable record of every single transaction ever made. Blockchain lays the base for a democratic open and scalable digital economy. ” This is a very dense definition, so let me try loosen things up. A database is a collection of information organized in such a way that a computer can quickly store, search and select desired pieces of data. An employer might use a database to store information about his employees, like name, birth date, contact address. In the case of a blockchain, the database is a record of all digital events like financial transactions or smart contracts. A blockchain is a special database, since it’s distributed. This means that the blockchain is not located on only one system, but rather replicated across a number of systems in almost real time. Usually it exists over a Peer-to-Peer (P2P) network, a network where connected nodes (“peers”) share resources amongst each other without the use of a centralized administrative system. This is also referred to as a decentralized network and is used by “services” like BitTorrent. In a network, based on the client-server model, the individual clients request services and resources from centralized servers. Seems familiar, right? It should be, because when you want to view a website or watch a video, you and everyone else request these resources from a service provider like Google or Facebook. A lot of the internet’s services are centralized. Since the blockchain keeps record of digital events, we need to verify those. Imagine this common situation: Alice wants to send Bob 20$. Normally a trusted third-party like PayPal makes sure that Alice is actually Alice and that she has sufficient money to execute the transaction and transfers the money. But a blockchain is public. It uses cryptographic and digital signature to prove the identity, authenticity and enforce read and write access rights for the database.Besides that, verification has to occur without compromising the privacy of the digital assets and parties involved. In this example we will use 20$ worth of Bitcoins. Alice (sender) would sign the transaction with her private key to identify Alice as the owner of the money, and send it to Bob’s (receiver) public key. Bob then would verify Alice’s private key with her public key, to make sure that she has sufficient cryptocurrency (BTC). The transaction, represented online as a ‘block’, is broadcast to every participant in the network.However, the transaction itself is not stored in the block. Only the fingerprint (hash) is stored. The fingerprint is generated with a hash algorithm: It turns data into a fixed-length hash like “85xj0d”: Modifying the data by one bit, for example turning a 0 to a 1 or the other way around, will change the hash completely.Those in the network will approve the transaction and add the verified block to the blockchain. The money will move now from Alice to Bob. There are two different blockchain types: public and private. In a public blockchain anyone can write and read without the permission of another authority. In a “private” blockchain the participants are known and trusted: for example, a company group. Many mechanisms like the following wouldn’t be needed - or are replaced with legal contracts. There are specific mechanisms that make manipulation and tampering with the blockchain next to impossible and very easy to detect when someone tries to. These mechanism would be Proof-of-Work (Bitcoin), Proof-of-Stake (Peercoin) and Delegate Proof-of-Stake (Shift), just to name a few. After having a brief look at blockchain in general, let’s look at a blockchain on a computer: A blockchain itself is just a file. Described as above, it is most closely comparable with a database. As the name blockchain says, it is made out of blocks chained together. You can imagine a block as a page in a book. A page contains the text (Story) and information about the page, for example a title or the page number. A block is similar: The content of the blocks are details about the digital events, while the ‘header’ contains information about the block itself, for example a reference to the previous block and the hash of the data contained (fingerprint). The hash has an important role for ordering the blocks and securing the blockchain. While a book orders the pages by building on the page whose number is one less (Page 5 builds on page 4), the blockchain orders the blocks by referencing the fingerprint (hash) of the previous block. If an attacker changes a block in the blockchain, you would inevitable change the hash. This would mean that the whole blockchain needs to be regenerated again, since they are all connected to each other. As mentioned earlier, a Blockchain is an over Peer-to-Peer network distributed database. However, there can be a few problems: How do you ensure that the system can’t be (easily) corrupted by bad peers? If each peer is updating at different speeds and have therefor have slightly different states, how do you determine the “true” state of the data? And what if multiple miners create different blocks at roughly the same time? Which block will count as the legit one that should count? Bitcoin solves the last named problem with the “longest chain rule”: Let’s say the first Block 1 you see is valid, which would be Block 1a. You then start building Block 2a, the next block on top of Block 1a. However when you see Block 1b, you keep an eye on it. If you see Block 2b later, the “longest chain rule” would say that the longer ‘b’ chain is the valid one, and the shorter ones should be ignored (‘a’). You then stop building Block 2a and start building 2b. Let’s say you would want to change that rule, because you have found a better way to choose the valid chain. In a centralized or private blockchain network this wouldn’t be a problem, since there is someone to enforce the changes. But in a public and “uncontrolled” network, enforcing changes is much more challenging. With Bitcoin, there are two parts to upgrading: First of all you would suggest the change as BIP (Bitcoin Improvement Proposal) and then miners can adopt to it by upgrading. This means, if the majority of miners chooses to upgrade to a new version, the new version of blocks would be created faster than the minority, which would force them to either switch or become an “irrelevant” blockchain fork. Therefor miners with lots of computational power more or less decide if a protocol or function gets implemented or not. We already covered that there are private and public blockchains, but the problem with a public network is while anyone can join, anyone can also start an attack. But what can a dishonest miner (attacker) do? While he can refuse to relay valid transactions to other nodes, attempt to create blocks that include or exclude specific transactions of his choosing and attempt to create a “longer chain” to exclude previously accepted blocks from the main chain, he cannot create bitcoins out of thin air, steal bitcoins from other accounts or make payments on someones behalf pretending to be someone else. Let me explain the consequences of each attack. If only one node in the network refuses to relay valid transactions, the other honest nodes will pass around the valid transactions and reject invalid transactions from the dishonest miner. And if he tries to exclude a transaction from a block, he would be only able to delay the transaction, since the other honest nodes would pickup this “unconfirmed transaction” in their blocks. However, an attacker can create a longer chain of blocks than the rest of the network to kick out the shorter chains. This lets the attacker unwind transactions. What would a dishonest miner need to do to unwind transactions? He would create two payments with the same bitcoins: One to an online retailer, the other to another address he controls. After creating both transactions he only broadcasts the payment that pays the retailer, who would send him the goods if the payment is included in an honest block. After the retailer send you the goods, you would secretly create a longer chain of blocks which exclude the payment to the retailer and include the payment to yourself. If the other nodes are honest and apply the “longest chain rule” on your published and manipulated chain, they will ignore the honest block and continue to build on your chain. The honest block is now orphaned. However, since you already spend the same bitcoins, the honest block is deemed invalid. This is called “double spend” and only appears for ledgers where block-makers aren’t trusted. To prevent attacks like this, you want to make it hard, expensive and next to impossible for malicious attackers to add blocks. Now the mechanism mentioned above come into play: Proof-of-Work (Bitcoin), Proof-of-Stake (Peercoin) and Delegate Proof-of-Stake (Shift) A consensus mechanism is an algorithm, that “decides” which block is valid, and which one isn’t. Through this, blockchain and these algorithms solve the Byzantine Generals’ Problem, which is an old computer science problem that was thought to be unsolvable before Bitcoin, the combination of blockchain and Proof-of-Work. Imagine this situation in the middle ages: There are 300 enemies in a castle surrounded by five armies of 100 men, each commanded by a general. If all five armies attacked at once, they overpower the 300 men inside the Castle. If the don’t attack simultaneously, there is the risk that the attacker will be outnumbered and lose the battle. As you can see, the generals need to agree when to attack, let’s say at 9pm. However, the “9pm attack” message could only be passed on by a rider on horseback. He has to ride around visiting each general in turn to confirm. Besides that, any general may be a traitor and in league with the enemies in the castle. What happened before blockchain and Proof-of-Work? General 1 decides to attack at 9pm. He sends out the message “9pm attack” to general 2 with his rider. General 2 reads the message, writes down the message and signs it to also say “9pm attack”. He sends the rider with the message to General 3. But General 3 is a traitor, and wants the attack to fail. He destroys the message that says “9pm attack” and replaces it with a message that says “8pm attack” and sends it to General 4. General 4 reads the message saying “8pm attack”. He then notes the time, signs the message and sends it to General 5. How does the attack look right now? General 4 and 5, that have 200 men together, attack the enemies at 8pm, that have 300 men. Because General 1 and 2 will attack only attack at 9pm, General 4 and 5 are outnumbered and the enemies win the first fight. At 9pm, General 3 joins the enemies and General 1 and 2, having 200 men, find themselves in a fight against 400 men now. Result: the (traitor) enemies win, since the generals could not find an agreement. What would happen with blockchain and Proof-of-Work? General 1 sends the same message (“attack at 9pm”), but now there are two more rules: Every General must spend 10 minutes preparing any new message for it to be valid and he must include the history of every previous message in every message. General 1 sends his message to General 2. This time General 2 know two things for certain: The message must have taken 10 minutes to prepare and that there are no previous messages - so it must be the truth. (Even if General 1 would be a traitor and puts in the wrong time, it doesn’t matter, because if the majority of Generals followed this suggestion, they will still outnumber those in the castle and win.) Now General 2 sends his message. As required, he spends 10 minutes preparing the new message and he embeds the message from General 1 into his own. The two messages are now 'chained’ and the rider sets off to General 3. General 3 is once more a traitor. Last time he changed the message to “8pm attack” so that General 4 and 5 would attack early and get outnumbered. But now he can’t. Why? Because he has only 10 minutes to prepare the message for General 4. He has two options now: He could try changing the message to “8pm attack”, but to do this, he first (a) needs to spend 10 minutes to prepare his own message, (b) 2 x 10 minutes to replace the messages of General 1 and General 3 and © he needs to carry out 30 minutes of work in 10 minutes, because otherwise the other Generals would know that he is traitor. Or he could accept his defeat and prepare the “9pm attack” message during the next 10 minutes. Result: The attackers outnumber the enemies win. This is one solution to the Byzantine Generals’ Problem. The 10 minutes of work example is also known as Proof-of-Work. Proof-of-Work is currently the most common consensus algorithm for blockchain technologies, including the Bitcoin. A proof of work is a piece of data which is difficult (costly and time-consuming) to produce, but easy for others to verify. Producing such data can be a random process that requires a lot of trial and error before a valid proof of work is generated. This work is done by the “miners”. For a block to be valid it must hash to a value lower or equal than the current target. The target is a 256-bit (SHA-256) number that all clients share. A hash algorithm turns data into a fixed-length hash and modifying the data by one bit, for example turning 0 to 1 or the other way around, will change the hash completely. Since each block contains the hash of the preceding block, each block has a chain of blocks connected to it that together contain a lot of work. The attacker must now “race" against the honest nodes: He has to do all the work faster then the rest of the honest nodes, and therefor have a higher hashrate, which comes with extreme hardware requirements and high energy consumption and is there for expensive and next to impossible. (find x such that f(block x) < t (cryptographic hash) However, Proof-of-Work also has problems for honest miner: Due to the computational resources needed, this consensus algorithm (PoW) is very energy-intensive, which can cause the miners to concentrate to one point, where mining is more efficent (mining pool) and energy isn’t expensive (China). This partially defeats the goal of decentralization. Proof-of-Stake is an alternative consensus algorithm for blockchains, that attempts to solve the issues associated with the “mining” process: Environmental damage through hardware and electricity and the overall electricity consumption, by removing the concept of “mining” entirely. Peercoin for example uses this algorithm. The mechanism in Proof of Stake can be described as a form of “virtual mining”: Instead of relying on computer hardware like Proof of Work does, Proof of Stake relies on the coins inside the blockchain. Think of it this way: With Proof of Work, a user buys a mining computer for 1000$, plugs it in and starts mining. The above described issues would occur. With Proof-of-Stake, a user would buy coins worth 1000$ and deposit them into the Proof of Stake mechanism and become validator. The PoS mechanism will then (pseudo-)randomly assign a validator the right to produce blocks. If that validator does not create a block with a period of time, a second validator is selected that can create the block instead. Note that deposing 10x more coins into the mechanism, will also increase the chance of becoming a validator by 10x and thus earn around 10x more rewards. This is similar as investing into mining hardware. Through this mechanism less quantity of energy of electricity is required in order to secure a blockchain and the centralization risks are reduced, since economies like energy and hardware costs are much less of a factor compared to PoW. Shift uses the Delegate Proof of Stake algorithm. dPoS tries to moderate the potential negative impacts of centralization through the use of witnesses, also known as delegates. There are 101 witnesses (SHIFT), which are voted on by those using the network, that sign the blocks. Additionally, each block signed must have a verification that the block before it was signed by a trusted node. By using a decentralized voting process, dPoS is by design more democratic than comparable systems: Instead of eliminating the need for trusting all nodes, dPoS has safeguards to ensure that the trusted delegates are signing the blocks correctly. Through the elimination of the need to wait until a certain number of untrusted nodes have verified a transaction before it can be confirmed and the ability of the blocks to contain more digital events, there is an increase in speed of transaction times compared to PoS and PoW. While dPoS focuses to be decentralized, it still has some controlled centralization. Every client in a dPoS system has the ability to decide who is trusted and become a delegated representative of the majority of users themselves, rather than trust concentrating in the hands of those with the most resources as it is with Proof-of-Work. But how does voting work? Every shareholder gets to vote for a representative to sign blocks in their stead. Anyone who can gain 1% or more of the votes can join the “board”. The representatives become a “board of directors”, signing blocks. If one of the directors misses their turn, clients will automatically devote them and eventually someone else will join. Just like other mechanism, the delegates are rewarded to make it worth their time ensuring uptime. In a nutshell, dPoS tries to give shareholders a way to delegate their vote to a key (without virtual mining), maximize the dividends shareholders earn and the performance, while minimizing the amount paid to secure the network and keep it running.